package com.hr.mes.controller;

import com.hr.mes.dto.Page;
import com.hr.mes.dto.Result;
import com.hr.mes.entity.FormFields;
import com.hr.mes.enums.MyExceptionEnum;
import com.hr.mes.exception.MyException;
import com.hr.mes.service.FirstItemService;
import com.hr.mes.service.ProcessService;
import com.hr.mes.vo.FormDetailVO;
import com.hr.mes.vo.FormSubmitVO;
import com.hr.mes.vo.FormViewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "首件记录")
@CrossOrigin
@RequestMapping("/api/firstItem")
@RestController
public class FirstItemController {

    @Autowired
    private ProcessService processService;

    @Autowired
    private FirstItemService firstItemService;

    @RequiresPermissions("firstItem:view")
    @ApiOperation("获取所有提交的表单")
    @GetMapping("getFormSubmitPage")
    public Result getFormSubmitPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                                    String employeeNumber, Integer status,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime){
        try{
            Page<FormViewVO> submitForms = firstItemService.getFormSubmitPage(pageNo, pageSize, employeeNumber, status, startTime, endTime);
            return Result.ok(submitForms);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.FIRSTITEM_VIEW_ERROR, e);
        }
    }

    @RequiresPermissions("firstItem:add")
    @ApiOperation("获取表单字段(非可重复添加部分)")
    @GetMapping("getFormFields")
    public Result getFormFields(Integer formTemplateId){
        try{
            List<FormFields> formFieldsList = firstItemService.getFormFields(formTemplateId);
            return Result.ok(formFieldsList);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.FIRSTITEM_ADD_ERROR, e);
        }
    }

    @RequiresPermissions("firstItem:add")
    @ApiOperation("获取可重复添加的表单字段")
    @GetMapping("getFormFieldsRepeat")
    public Result getFormFieldsRepeat(Integer formTemplateId){
        try{
            List<FormFields> formFieldsList = firstItemService.getFormFieldsRepeat(formTemplateId);
            return Result.ok(formFieldsList);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.FIRSTITEM_ADD_ERROR, e);
        }
    }

    @RequiresPermissions("firstItem:add")
    @ApiOperation("提交首件记录")
    @PostMapping("addFirstItem")
    public Result addFirstItem(@RequestBody FormSubmitVO formSubmitVO){
        try{
            Integer formId = firstItemService.addFirstItem(formSubmitVO);
            String taskId = processService.startProcessForService("firstItem", formId);
            processService.completeTask(formId, taskId, formSubmitVO.getNextAssignee());
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.FIRSTITEM_ADD_ERROR, e);
        }
    }

    @RequiresPermissions("firstItem:view")
    @ApiOperation("查询表单详情")
    @GetMapping("getFormDetails")
    public Result getFormDetails(@RequestParam Integer formId){
        try{
            List<FormDetailVO> formDetailVOs = firstItemService.getFormDetails(formId);
            return Result.ok(formDetailVOs);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.FIRSTITEM_VIEW_ERROR, e);
        }
    }
}
